package dev.touchswipe.ScreenLock
{
	import dev.touchswipe.shared.Fonts;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.Timer;
	
	public class Clock extends Sprite
	{
		private var _timer:Timer;
		private var _textField:TextField;
		
		private static const format1:TextFormat=new TextFormat(Fonts.SegoeUIL,80,0xffffff,null,null,null,null,null,TextFormatAlign.CENTER,null,null,null,-20);
		private static const format2:TextFormat=new TextFormat(Fonts.SegoeUIL,40,0xffffff,null,null,null,null,null,TextFormatAlign.CENTER);
		
		public function Clock()
		{
			super();
			_timer=new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER,_onTimer);
			_timer.start();
			_textField=new TextField();
			addChild(_textField);
			_textField.selectable=false;
			_textField.height=500;
		}
		
		public override function set width(value:Number):void{
			_textField.width=value;
		}
		
		private function _onTimer(event:TimerEvent):void{
			var date:Date=new Date();
			var timeString:String=lpad(date.hours)+":"+lpad(date.minutes)+":"+lpad(date.seconds);
			var dateString:String=lpad(date.date)+"/"+lpad(date.month+1)+"/"+date.fullYear;
			_textField.text=timeString+"\r"+dateString;
			_textField.setTextFormat(format1,0,timeString.length);
			_textField.setTextFormat(format2,timeString.length+1,_textField.text.length);
			_textField.embedFonts=true;
			_textField.antiAliasType=AntiAliasType.ADVANCED;
		}
		
		private function lpad(x:Number,length:int=2,char:String="0"):String{
			var result:String=x.toString();
			while(result.length<length){
				result=char+result;
			}
			return result;
		}
	}
}